﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.IO;
using Microsoft.Win32;

namespace WindowsFormsApplication1
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
            cleanButton.Enabled = false;

        }



        private void cleanButton_Click(object sender, EventArgs e)
        {
            string sourcePath = Environment.GetEnvironmentVariable("TMP");
            ClearFolder(sourcePath);
            DirSearchAndDisplay(sourcePath);
            if (listBox1.Items.Count > 0)
            {
                cleanButton.Enabled = true;
            }
        }

        private void ClearFolder(string FolderName)
        {
            DirectoryInfo dir = new DirectoryInfo(FolderName);

            foreach (FileInfo fi in dir.GetFiles())
            {
                fi.IsReadOnly = false;
                fi.Delete();
            }

            foreach (DirectoryInfo di in dir.GetDirectories())
            {
                ClearFolder(di.FullName);
                di.Delete();
            }
        }

        private void tempFilesButton_Click_1(object sender, EventArgs e)
        {
            listBox1.Items.Clear();
            string sourcePath = Environment.GetEnvironmentVariable("TMP");
            this.DirSearchAndDisplay(sourcePath);
            if (listBox1.Items.Count > 0)
            {
                cleanButton.Enabled = true;
            }
        }

        private void DirSearchAndDisplay(string sDir)
        {
            cleanButton.Enabled = false;
            try
            {
                foreach (string f in Directory.GetFiles(sDir))
                {
                    listBox1.Items.Add(f);
                }

                foreach (string d in Directory.GetDirectories(sDir))
                {
                    this.DirSearchAndDisplay(d);
                }
            }
            catch (System.Exception excpt)
            {
                listBox1.Items.Add(excpt.Message);
            }
        }

        private void uninstallButton_Click(object sender, EventArgs e)
        {
            String str = (listBox1.SelectedIndex).ToString();
            GetUninstallCommandFor(str);
        }

        private void listBox1_SelectedIndexChanged(object sender, EventArgs e)
        {
            uninstallButton.Enabled = true;
        }

        private void Installed()
        {
            string[] Software = new string[3];

            string SoftwareKey = @"SOFTWARE\Microsoft\Windows\CurrentVersion\Uninstall";
            using (RegistryKey rk = Registry.LocalMachine.OpenSubKey(SoftwareKey))
            {

                foreach (string skName in rk.GetSubKeyNames())
                {
                    using (RegistryKey sk = rk.OpenSubKey(skName))
                    {
                        try
                        {

                            if (!(sk.GetValue("DisplayName") == null))
                            {
                                Software[0] = sk.GetValue("DisplayName").ToString();
                                Software[1] = sk.GetValue("Publisher").ToString();
                                Software[2] = sk.GetValue("InstallLocation").ToString();
                                listView1.Items.Add(new ListViewItem(Software));
                            }
                        }
                        catch (Exception ex)
                        {
                            string store = ex.Message;
                        }
                    }
                }
            }
        }

        private void Form1_Load(object sender, EventArgs e)
        {
            uninstallButton.Enabled = true;
            Installed();
        }

        public string GetUninstallCommandFor(string productDisplayName)
        {
            uninstallButton.Enabled = true;
            RegistryKey localMachine = Registry.LocalMachine;
            string productsRoot = @"SOFTWARE\Microsoft\Windows\CurrentVersion\Installer\UserData\S-1-5-18\Products";
            RegistryKey products = localMachine.OpenSubKey(productsRoot);
            string[] productFolders = products.GetSubKeyNames();

            foreach (string p in productFolders)
            {
                RegistryKey installProperties = products.OpenSubKey(p + @"\InstallProperties");
                if (installProperties != null)
                {
                    string displayName = (string)installProperties.GetValue("DisplayName");
                    if ((displayName != null) && (displayName.Contains(productDisplayName)))
                    {
                        string uninstallCommand = (string)installProperties.GetValue("UninstallString");
                        return uninstallCommand;
                    }
                }
            }
            return "";
        }
    }
}
